<?php
    $n = ceil($count / $per_page);

    $show_num = 2;
?>
<?php if ($n > 1): ?>
<div class="pagination">

    <?php if ($page != 1): ?><a href="<?=$url . '/page/' . ($page - 1) ?>" class="button button_gray">‹</a><?php endif; ?>
    <?php if ($page - $show_num > 1): ?>
        <a class="page" href="<?=$url . '/page/1' ?>">1</a>
        <span>...</span>
        <?php for ($i = $page - $show_num; $i < $page; $i++): ?>
        <a class="page" href="<?=$url . '/page/' . $i ?>"><?=$i ?></a>
        <?php endfor; ?>
        <?php else: ?>
        <?php for ($i = $page - $show_num; $i < $page; $i++): ?>
        <?php if ($i < 1) {continue;} ?> 
        <a class="page" href="<?=$url . '/page/' . $i ?>"><?=$i ?></a> 
        <?php endfor; ?>
        <?php endif; ?>
        
        <span class="current page"><?=$page ?></span>
        
        <?php if ($page + $show_num < $n): ?>
        <?php for ($i = $page + 1 ; $i < $page + $show_num + 1; $i++): ?>
        <a class="page" href="<?=$url . '/page/' . $i ?>"><?=$i ?></a> 
        <?php endfor; ?>
        
        <span>...</span>
        
        <a class="page" href="<?=$url . '/page/' . $n ?>"><?=$n ?></a> 
        <?php else: ?>
        <?php for ($i = $page + 1 ; $i < $page + $show_num + 1; $i++): ?>
        <?php if ($i > $n) {continue;} ?> 
        <a class="page" href="<?=$url . '/page/' . $i ?>"><?=$i ?></a>
        <?php endfor; ?>
        <?php endif; ?>
        
        <?php if ($page != $n): ?><a class="button" href="<?=$url . '/page/' . ($page + 1) ?>">Далее&nbsp;›</a><?php endif; ?>
     
</div>
<?php endif; ?>